package com.songVersion.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.songVersion.model.Thumbnail;
import com.songVersion.model.ThumbnailCache;
import com.songVersion.model.YoutubeInfo;
import com.songVersion.model.YoutubeInfoCache;
import com.songVersion.services.YoutubeInfoManager;
import com.songVersion.utils.PMF;

public class YoutubeInfoManagerImpl extends ManagerImpl<YoutubeInfoCache>
		implements YoutubeInfoManager
{

	
	
	@SuppressWarnings("unchecked")
	@Override
	public YoutubeInfoCache loadFromPattern(String pattern)
	{
		System.out.println("pattern => " + pattern);
		YoutubeInfoCache info = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(YoutubeInfoCache.class);
		query.setFilter("pattern == patternParam");
		query.declareParameters("String patternParam");
		List<YoutubeInfoCache> l = (List<YoutubeInfoCache>) query
				.execute(pattern);
		System.out.println(l.size());
		YoutubeInfoCache infoRes = null;
		if (l.size() > 0)
		{
			info = l.get(0);
			// Creamos una copia del objeto porque si no se perdian la lista de
			// thumbnails
			infoRes = new YoutubeInfoCache(info);
		}
		pm.close();
		return infoRes;
	}

	@Override
	public void init()
	{
		this.t = new YoutubeInfoCache();

	}

	@Override
	public YoutubeInfo fromCache(YoutubeInfoCache cache)
	{

		YoutubeInfo res = new YoutubeInfo();
		res.setKey(cache.getKey());
		res.setInsertDate(cache.getInsertDate());
		res.setPattern(cache.getPattern());
		res.setPlayerUrl(cache.getPlayerUrl());
		res.setVideoId(cache.getVideoId());
		List<Thumbnail> thumbs = new ArrayList<Thumbnail>();
		// System.out.println(info.getThumbnails());
		for (ThumbnailCache t : cache.getThumbnails())
		{
			Thumbnail t2 = new Thumbnail();
			t2.setHeight(t.getHeight());
			t2.setKey(t.getKey());
			t2.setUrl(t.getUrl());
			t2.setWidth(t.getWidth());
			thumbs.add(t2);
		}
		res.setThumbnails(thumbs);
		return res;
	}

	@Override
	public YoutubeInfoCache toCache(YoutubeInfo info)
	{
		YoutubeInfoCache res = new YoutubeInfoCache();
		res.setKey(info.getKey());
		res.setInsertDate(info.getInsertDate());
		res.setPattern(info.getPattern());
		res.setPlayerUrl(info.getPlayerUrl());
		res.setVideoId(info.getVideoId());
		List<ThumbnailCache> thumbs = new ArrayList<ThumbnailCache>();
		// System.out.println(info.getThumbnails());
		for (Thumbnail t : info.getThumbnails())
		{
			ThumbnailCache t2 = new ThumbnailCache(t);
			/*
			t2.setHeight(t.getHeight());
			t2.setKey(t.getKey());
			t2.setUrl(t.getUrl());
			t2.setWidth(t.getWidth());
			*/
			thumbs.add(t2);
		}
		res.setThumbnails(thumbs);
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public YoutubeInfoCache loadByVideoId(String videoId)
	{
		YoutubeInfoCache info = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(YoutubeInfoCache.class);
		query.setFilter("videoId == videoIdParam");
		query.declareParameters("String videoIdParam");
		List<YoutubeInfoCache> l = (List<YoutubeInfoCache>) query
				.execute(videoId);
		YoutubeInfoCache infoRes = null;
		if (l.size() > 0)
		{
			info = l.get(0);
			// Creamos una copia del objeto porque si no se perdian la lista de
			// thumbnails
			infoRes = new YoutubeInfoCache(info);
		}
		pm.close();
		return infoRes;
	}
}
